using System;
using System.Collections;
using System.Xml;

namespace AleaClasses
{
	/// <summary>
	/// TLabels is a collection of TLabel
	/// </summary>
	public class TLabels : CollectionBase, ITrackEdits
	{
		private TLabelsTrackEdits fLabelsTrackEdits;

		private void Constructor()
		{
			fLabelsTrackEdits = new TLabelsTrackEdits(this);
		}

		public TLabels(): base()
		{
			Constructor();
		}

		public TLabels(XmlElement aLabelsElem): base()
		{
			Constructor();

			foreach(XmlElement lLabelElem in aLabelsElem)
			{
				TLabel lLabel = new TLabel(lLabelElem);
				this.Add(lLabel);
			}
		}

		public XmlElement SaveToXml(XmlDocument aDoc)
		{
			XmlElement retval = null;
			XmlElement elem = null;

			retval = aDoc.CreateElement("Labels");

			foreach(TLabel lLabel in this)
			{
				elem = lLabel.SaveToXml(aDoc);
				if (elem != null)
					retval.AppendChild(elem);
			}

			return retval;
		}

		public TLabel this[ int index ]  
		{
			get  
			{
				return( (TLabel) List[index] );
			}
			set  
			{
				if (List[index] != value)
				{
					List[index] = value;
					fLabelsTrackEdits.DoSetNeedsSave();
				}	
			}
		}

		public int Add( TLabel value )  
		{
			int retval = List.Add( value );
			fLabelsTrackEdits.DoSetNeedsSave();
			return( retval );
		}

		public int IndexOf( TLabel value )  
		{
			return( List.IndexOf( value ) );
		}

		public void Insert( int index, TLabel value )  
		{
			List.Insert( index, value );
			fLabelsTrackEdits.DoSetNeedsSave();
		}

		public void Remove( TLabel value )  
		{
			List.Remove( value );
			fLabelsTrackEdits.DoSetNeedsSave();
		}

		public bool Contains( TLabel value )  
		{
			// If value is not of type TLabel, this will return false.
			return( List.Contains( value ) );
		}

		protected override void OnInsert( int index, Object value )  
		{
			if ( value.GetType() != Type.GetType("AleaClasses.TLabel") )
				throw new ArgumentException( "value must be of type TLabel.", "value" );
		}

		protected override void OnRemove( int index, Object value )  
		{
			if ( value.GetType() != Type.GetType("AleaClasses.TLabel") )
				throw new ArgumentException( "value must be of type TLabel.", "value" );
		}

		protected override void OnSet( int index, Object oldValue, Object newValue )  
		{
			if ( newValue.GetType() != Type.GetType("AleaClasses.TLabel") )
				throw new ArgumentException( "newValue must be of type TLabel.", "newValue" );
		}

		protected override void OnValidate( Object value )  
		{
			if ( value.GetType() != Type.GetType("AleaClasses.TLabel") )
				throw new ArgumentException( "value must be of type TLabel." );
		}

		public TLabel GetLabelByLabelCode(string aLabelCode)
		{
			TLabel retval = null;

			foreach(TLabel Label in this)
			{
				if (Label.LabelCode.ToUpper().Equals(aLabelCode.ToUpper()))
				{
					retval = Label;
				}
			}

			return retval;
		}
		#region ITrackEdits Members

		public bool NeedsSave()
		{
			return fLabelsTrackEdits.NeedsSave();
		}

		public void ClearNeedsSave()
		{
			fLabelsTrackEdits.ClearNeedsSave();
		}

		#endregion
	}

	public class TLabelsTrackEdits: TTrackEdits
	{
		private TLabels fLabels;

		public TLabelsTrackEdits(TLabels aLabels)
		{
			fLabels = aLabels;
		}

		protected override void ClearChildrenNeedsSave()
		{
			foreach (ITrackEdits te in fLabels)
			{
				te.ClearNeedsSave();
			}
		}

		protected override bool ChildrenNeedSave()
		{
			bool retval = false;

			foreach (ITrackEdits te in fLabels)
			{
				retval |= te.NeedsSave();
			}

			return retval;
		}
		
		internal void DoSetNeedsSave()
		{
			this.SetNeedsSave();
		}
	}
}
